package com.winphone.uavc.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winphone.uavc.common.result.Result;
import com.winphone.uavc.entity.SysUser;
import com.winphone.uavc.mapper.SysUserMapper;
import com.winphone.uavc.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwl
 * @since 2017-12-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 分页查询
     * @param pageInfo
     * @param map
     * @return
     */
    @Override
    public Page<SysUser> selectPageParam(Page<SysUser> pageInfo, Map map) {
        List<SysUser> sysUsers = sysUserMapper.selectPageParam(pageInfo,map);
        pageInfo.setRecords(sysUsers);
        return pageInfo;
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @Override
    public SysUser getSysUserInfoById(Long id) {
        //查询用户信息
        return sysUserMapper.getSysUserInfoById(id);
    }

    /**
     * 根据用户姓名查询用户信息
     * @param userName
     * @return
     */
    @Override
    public SysUser getSysUserInfoByUserName(String userName) {
        //查询用户信息
        return sysUserMapper.getSysUserInfoByUserName(userName);
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = {})
    public boolean updateSysUserInfo(SysUser sysUser) {
        boolean result = true;
        int updateResult = sysUserMapper.updateSysUserInfo(sysUser);
        if(updateResult < 0){
            return false;
        }
        return result;
    }

    /**
     * 修改密码
     * @param userId
     * @param newPassword
     * @param phone
     * @return
     */
    @Override
    public Result updatePassword(Long userId,String newPassword,String phone) {
        SysUser sysUser = sysUserMapper.getSysUserInfoById(userId);
        if(sysUser == null){
            return new Result(Result.FAILURE,Result.STR_FAIL);
        }
        sysUser.setPassword(newPassword);
        sysUser.setPhone(phone);
        //修改密码
        sysUserMapper.updateSysUserInfo(sysUser);
        return new Result(Result.SUCCESS,Result.STR_SUCCESS,true);
    }

    /**
     *  用户软删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Long id) {
        int deleteResult = sysUserMapper.deleteById(id);
        return deleteResult >= 0;
    }


    /**
     * 唯一性验证
     */
    @Override
    public boolean selectByMapOnce(Map<String, Object> map) {
        List<SysUser> list = sysUserMapper.selectByMapOnce(map);
        return list == null || list.isEmpty();
    }

}
